package com.yycx.dbr.boot.admin.handler;

import com.yycx.common.base.handler.AdminLoginHandler;
import com.yycx.common.base.module.LoginParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;
import java.util.Map;

/**
 * @author Administrator
 */
@Component
public class MyAdminLoginHandler implements AdminLoginHandler {

    @Autowired
    private BaseUserService userService;

    @Override
    public ResultBody beforLogin(LoginParams loginParams) {
        Long companyId = loginParams.getCompanyId();
        ApiAssert.isNotEmpty("账套ID不能为空", companyId);
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterLogin(Map<String, Object> map, LoginParams loginParams) {
        Long companyId = loginParams.getCompanyId();
        String hostName = loginParams.getHostName().toUpperCase();
        Long userId = Long.parseLong(map.get("openid").toString());
        BaseUser baseUser = userService.getById(userId);
        baseUser.setCompanyId(companyId);
        //baseUser.setHostName(hostName);
        userService.updateById(baseUser);
        return ResultBody.ok();
    }
}
